package com.ruoyi.spicfz.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.spicfz.domain.Company;
import com.ruoyi.spicfz.domain.Shareholder;
import com.ruoyi.spicfz.mapper.CompanyMapper;
import com.ruoyi.spicfz.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 公司信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@Service("company")
public class CompanyServiceImpl implements ICompanyService 
{
    @Autowired
    private CompanyMapper companyMapper;

    /**
     * 查询公司信息
     * 
     * @param companyId 公司信息ID
     * @return 公司信息
     */
    @Override
    public Company selectCompanyById(Long companyId)
    {
        return companyMapper.selectCompanyById(companyId);
    }

    /**
     * 查询公司信息列表
     * 
     * @param company 公司信息
     * @return 公司信息
     */
    @Override
    public List<Company> selectCompanyList(Company company)
    {
        return companyMapper.selectCompanyList(company);
    }

    /**
     * 新增公司信息
     * 
     * @param company 公司信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCompany(Company company)
    {
        company.setCreateTime(DateUtils.getNowDate());
        int rows = companyMapper.insertCompany(company);
        insertShareholder(company);
        return rows;
    }

    /**
     * 修改公司信息
     * 
     * @param company 公司信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCompany(Company company)
    {
        company.setUpdateTime(DateUtils.getNowDate());
        companyMapper.deleteShareholderByCompanyId(company.getCompanyId());
        insertShareholder(company);
        return companyMapper.updateCompany(company);
    }

    /**
     * 删除公司信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCompanyByIds(String ids)
    {
        //companyMapper.deleteShareholderByCompanyIds(Convert.toStrArray(ids));
        return companyMapper.deleteCompanyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除公司信息信息
     * 
     * @param companyId 公司信息ID
     * @return 结果
     */
    @Override
    public int deleteCompanyById(Long companyId)
    {
        //companyMapper.deleteShareholderByCompanyId(companyId);
        return companyMapper.deleteCompanyById(companyId);
    }

    /**
     * 新增股东信息信息
     * 
     * @param company 公司信息对象
     */
    public void insertShareholder(Company company)
    {
        List<Shareholder> shareholderList = company.getShareholderList();
        Long companyId = company.getCompanyId();
        if (StringUtils.isNotNull(shareholderList))
        {
            List<Shareholder> list = new ArrayList<Shareholder>();
            for (Shareholder shareholder : shareholderList)
            {
                shareholder.setCompanyId(companyId);
                list.add(shareholder);
            }
            if (list.size() > 0)
            {
                companyMapper.batchShareholder(list);
            }
        }
    }
}
